package tableApi;

import beans.SenSorReading;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Over;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.Tumble;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Rowtime;
import org.apache.flink.table.descriptors.Schema;
import org.apache.flink.types.Row;

public class tabletest05_TimeAndWindow {
    public static void main(String[] args) throws Exception {
        //创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //2.读入文件数据,得到DataStream
        String filePath = "src/main/resources/sensor.txt";
        DataStreamSource<String> inputStream = env.readTextFile(filePath);

        //3.转换成POJO
        SingleOutputStreamOperator<SenSorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SenSorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        })
                .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<SenSorReading>(Time.seconds(2)) {
                    @Override
                    public long extractTimestamp(SenSorReading senSorReading) {
                        return senSorReading.getTimeStamp() * 1000l;
                    }
                });

        //4.将流转换成表，定义时间特性
//        Table dataTable = tableEnv.fromDataStream(dataStream, "id,timeStamp as ts ,temperature as temp,pt.proctime");
        Table dataTable = tableEnv.fromDataStream(dataStream, "id,timeStamp as ts ,temperature as temp,rt.rowtime");

        tableEnv.connect(new FileSystem().path(filePath))
                .withFormat(new Csv())//表的文件格式
                .withSchema(new Schema()
                        .field("id", DataTypes.STRING())
                        .field("timestamp", DataTypes.BIGINT())
                        .field("temperature", DataTypes.DOUBLE())
                        .field("pt", DataTypes.TIMESTAMP(3))
                        .proctime())
                .createTemporaryTable("input1");


        //定义Table Schema时指定
        tableEnv.connect(new FileSystem().path(filePath))
                .withFormat(new Csv())//表的文件格式
                .withSchema(new Schema()//表的数据结构,属性名是可以变更的，但是顺序时不可以变换的
                        .field("id", DataTypes.STRING())
                        .field("timeStamp", DataTypes.BIGINT())
                        .rowtime(
                                new Rowtime()
                                        .timestampsFromField("timeStamp")//从字段中提取时间戳
                                        .watermarksPeriodicBounded(1000l)//watermark延迟1秒
                        )
                        .field("temperature", DataTypes.DOUBLE()))
                .createTemporaryTable("input");
        Table inputTable = tableEnv.from("input");

        //5.操作窗口
        //5.1Group window
        Table resultTable = dataTable.window(Tumble.over("10.seconds").on("rt").as("tw"))//十秒钟事件语义下的滚动窗口
                .groupBy("id,tw")
                .select("id,id.count,temp.avg,tw.end");

        //SQL
        tableEnv.createTemporaryView("sensor", dataTable);
        Table resdultSQLTable = tableEnv.sqlQuery("select id,count(id) as cnt,avg(temp) as avgTemp, tumble_end(rt, interval '10' second) " +
                "from sensor group by id, tumble(rt, interval '10' second)");

//        dataTable.printSchema();
//        tableEnv.toAppendStream(dataTable, Row.class).print();
//        inputTable.printSchema();
//        tableEnv.toAppendStream(inputTable, Row.class).print();
//        tableEnv.toAppendStream(resultTable, Row.class).print("result");
//        tableEnv.toRetractStream(resdultSQLTable, Row.class).print("sql");

        //Over Window
        Table OverWindow = dataTable.window(Over.partitionBy("id").orderBy("rt").preceding("2.rows").as("ow"))
                .select("id,rt,id.count over ow,temp.avg over ow");
        Table sqlOverWindow = tableEnv.sqlQuery("select id,rt,count(id) over ow,avg(temp) over ow " +
                "from sensor " +
                "window ow as (partition by id order by rt rows between 2 preceding and current row)");

        tableEnv.toAppendStream(OverWindow, Row.class).print("result");
        tableEnv.toRetractStream(sqlOverWindow, Row.class).print("sql");
        env.execute();
    }
}
